﻿using EMoney.IStock.Client.Business;
using EMoney.IStock.Client.Business.Tclz;
using EMoney.IStock.Client.Business.Yjjx;
using EMoney.IStock.Client.Framework;
using EMoney.IStock.Client.Models;
using EMoney.IStock.Client.Models.Tclz;
using EMoney.IStock.Client.Models.Yjjx;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.DataVisualization.Charting;

namespace EMoney.IStock.Client.Web.Controllers
{
    public class TcxgController : Controller
    {
        // 题材选股
        // GET: /Tcxg/

        #region 业绩精选
        /// <summary>
        /// 业绩精选
        /// </summary>
        /// <returns></returns>
        public ActionResult Yjjx()
        {
            if (!string.IsNullOrEmpty(Request["token"]))    //token有值时，执行认证操作
            {
                if (!TokenBll.CheckRightsByToken(Request["token"], FuncRights.YJJX))
                {
                    return null;
                }
            }
            else
            {
                if (!TokenBll.CheckRightsByCookie(FuncRights.YJJX))    //获取Cookie值验证是否有权限
                {
                    return null;
                }
            }

            string css = StringHelper.GetStaticFile("IStockClientStatic/tcxg/css/tcxg_tint.css");

            if (!string.IsNullOrEmpty(Request["color"]) && Request["color"] == "black")
            {
                css = StringHelper.GetStaticFile("IStockClientStatic/tcxg/css/tcxg.css");
                ViewBag.Color = "black";
            }
            else
            {
                ViewBag.Color = "default";
            }

            ViewBag.PageCss = css;

            //近两周日期
            ViewBag.RecentDate = new YjjxBll().GetRecentDate();

            return View();
        }

        /// <summary>
        /// 获取业绩精选股票池
        /// </summary>
        /// <param name="indate"></param>
        /// <param name="bltype"></param>
        /// <param name="sortFlag"></param>
        /// <param name="sortType"></param>
        /// <returns></returns>
        public string GetYjjxPool(string indate, string bltype = "0", string sortFlag = "InDate", string sortType = "desc")
        {
            if (!TokenBll.CheckRightsByCookie(FuncRights.YJJX))
            {
                return string.Empty;
            }

            if (string.IsNullOrEmpty(indate))
            {
                return string.Empty;
            }

            DateTime dtIndate = DateTime.MinValue;
            if (!DateTime.TryParse(indate, out dtIndate))
            {
                return string.Empty;
            }

            List<VM_PERFORMANCE_CHANGE_POOL_MAXDATE> list = new YjjxBll().GetPool(dtIndate, sortFlag, sortType, bltype);

            if (list != null && list.Count > 0)
            {
                return JsonHelper.ToJson(list);
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 业绩精选 详情
        /// </summary>
        /// <returns></returns>
        public ActionResult YjjxDetails()
        {
            if (!TokenBll.CheckRightsByCookie(FuncRights.YJJX))
            {
                return null;
            }

            string css = StringHelper.GetStaticFile("IStockClientStatic/tcxg/css/tcxg_tint.css");

            if (!string.IsNullOrEmpty(Request["color"]) && Request["color"] == "black")
            {
                css = StringHelper.GetStaticFile("IStockClientStatic/tcxg/css/tcxg.css");
                ViewBag.Color = "black";
            }
            else
            {
                ViewBag.Color = "default";
            }

            ViewBag.PageCss = css;

            return View();
        }

        /// <summary>
        /// 获取业绩精选战绩
        /// </summary>
        /// <param name="sortFlag"></param>
        /// <param name="sortType"></param>
        /// <returns></returns>
        public string GetRecord(string sortFlag, string sortType)
        {
            if (!TokenBll.CheckRightsByCookie(FuncRights.YJJX))
            {
                return string.Empty;
            }

            if (string.IsNullOrEmpty(sortFlag) || string.IsNullOrEmpty(sortType))
            {
                return string.Empty;
            }
            else
            {
                List<VM_PERFORMANCE_CHANGE_POOL_GOODRATIO> list = new YjjxBll().GetRecord(sortFlag, sortType);

                if (list != null && list.Count > 0)
                {
                    return JsonHelper.ToJson(list);
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        public FileContentResult ChartYjjx(string type)
        {
            if (!TokenBll.CheckRightsByCookie(FuncRights.YJJX))
            {
                return null;
            }

            double htoValue1 = 0;
            double htoValue2 = 0;
            string htoname1 = string.Empty;
            string htoname2 = string.Empty;
            string blType = string.Empty;

            YjjxBll yjjxBll = new YjjxBll();            

            switch (type)
            {
                case "6":
                    blType = "续盈";
                    break;
                case "7":
                    blType = "扭亏";
                    break;
                case "8":
                    blType = "减亏";
                    break;
                default:
                    break;
            }

            List<CircleRate> listhigh = yjjxBll.GetCircleRate(blType);

            if (listhigh != null && listhigh.Count > 0)
            {
                for (int i = 0; i < listhigh.Count; i++)
                {
                    if (i == 0)
                    {
                        htoname1 = listhigh[i].Name;
                        htoValue1 = listhigh[i].Value * 2;
                    }
                    else
                    {
                        htoname2 = listhigh[i].Name;
                        htoValue2 = listhigh[i].Value * 2;
                    }

                }
            }
            double sum = htoValue1 + htoValue2;

            Chart chart1 = new Chart();
            Color backColor = ColorTranslator.FromHtml("#171717");

            chart1.BackColor = Color.Transparent;
            chart1.Width = 140;
            chart1.Height = 160;

            chart1.ChartAreas.Add("area1");
            chart1.Series.Add("业绩精选");
            chart1.Series["业绩精选"].CustomProperties = "DoughnutRadius=45, PieDrawingStyle=Concave, CollectedLabel=Other, MinimumRelativePieSize=40";
            chart1.Series["业绩精选"].ChartType = SeriesChartType.Pie;
            chart1.Series["业绩精选"]["DoughnutRadius"] = "30";
            chart1.Series["业绩精选"]["PieLabelStyle"] = "Inside";

            DataPoint dp1 = new DataPoint();
            
            dp1 = new DataPoint();
            dp1.YValues = new double[] { htoValue1 };
    
            if (!string.IsNullOrEmpty(htoname1))
            {
                dp1.Label = (htoValue1 * 100 / sum).ToString().Split('.')[0] + "%";
                dp1.Font = new Font("Arial", 12);                
            }

            switch (type)
            {
                case "6":
                    dp1.Color = ColorTranslator.FromHtml("#DB7E91");
                    break;
                case "7":
                    dp1.Color = ColorTranslator.FromHtml("#BB7B60");
                    break;
                case "8":
                    dp1.Color = ColorTranslator.FromHtml("#768BCC");
                    break;
                default:
                    dp1.Color = ColorTranslator.FromHtml("#9F58B6");
                    break;
            }

            chart1.Series["业绩精选"].Points.Add(dp1);

            dp1 = new DataPoint();
            dp1.YValues = new double[] { htoValue2 };
            
            if (!string.IsNullOrEmpty(htoname2))
            {
                dp1.Label = (100 - Convert.ToInt32((htoValue1 * 100 / sum).ToString().Split('.')[0])) + "%";
                dp1.Font = new Font("Arial", 12);
            }

            switch (type)
            {
                case "6":
                    dp1.Color = ColorTranslator.FromHtml("#B72F4D");
                    break;
                case "7":
                    dp1.Color = ColorTranslator.FromHtml("#8D331C");
                    break;
                case "8":
                    dp1.Color = ColorTranslator.FromHtml("#3649A4");
                    break;
                default:
                    dp1.Color = ColorTranslator.FromHtml("#B27B3A");
                    break;
            }

            chart1.Series["业绩精选"].Points.Add(dp1);

            chart1.ChartAreas[0].BackColor = Color.Transparent;

            chart1.DataBind();
            MemoryStream memory = new MemoryStream();
            chart1.SaveImage(memory, ChartImageFormat.Png);
            var bdata = memory.ToArray();
            memory.Dispose();
            return new FileContentResult(bdata, "image/png");
        }
        #endregion

        #region 题材领涨
        /// <summary>
        /// 题材领涨
        /// </summary>
        /// <returns></returns>
        public ActionResult Tclz()
        {
            if (!string.IsNullOrEmpty(Request["token"]))    //token有值时，执行认证操作
            {
                if (!TokenBll.CheckRightsByToken(Request["token"], FuncRights.TCLZ))
                {
                    return null;
                }
            }
            else
            {
                if (!TokenBll.CheckRightsByCookie(FuncRights.TCLZ))    //获取Cookie值验证是否有权限
                {
                    return null;
                }
            }

            string css = StringHelper.GetStaticFile("IStockClientStatic/tcxg/css/tcxg_tint.css");

            if (!string.IsNullOrEmpty(Request["color"]) && Request["color"] == "black")
            {
                css = StringHelper.GetStaticFile("IStockClientStatic/tcxg/css/tcxg.css");
                ViewBag.Color = "black";
            }
            else
            {
                ViewBag.Color = "default";
            }

            //TokenBll.CheckToken(Request["token"],FuncRights.TCLZ);

            ViewBag.PageCss = css;
            ViewBag.Token = Request["token"];

            //近半年月份
            ViewBag.RecentMonth = new TclzBll().GetRecentMonth();

            return View();
        }

        /// <summary>
        /// 获取题材领涨股票池
        /// </summary>
        /// <returns></returns>
        public string GetTclzPool()
        {
            if (!TokenBll.CheckRightsByCookie(FuncRights.YJJX))
            {
                return string.Empty;
            }

            string reqMonth = Request["m"];

            if (string.IsNullOrEmpty(reqMonth))
            {
                return string.Empty;
            }

            TclzBll bll = new TclzBll();

            DateTime dtBegin = DateTime.MinValue;
            DateTime dtEnd = DateTime.MinValue;

            StringHelper.FormatMonth(reqMonth, out dtBegin, out dtEnd);

            if (dtBegin == DateTime.MinValue || dtEnd == DateTime.MinValue)
            {
                return string.Empty;
            }

            int totalNumber = bll.GetTotalNumber(dtBegin, dtEnd);
            List<VM_CHASE_STK_LIST> list = bll.GetList(dtBegin, dtEnd);

            return string.Concat(totalNumber + "^" + JsonHelper.ToJson(list));
        }

        /// <summary>
        /// 题材领涨详情
        /// </summary>
        /// <returns></returns>
        public ActionResult TclzDetails()
        {
            if (!TokenBll.CheckRightsByCookie(FuncRights.YJJX))
            {
                return null;
            }

            string css = StringHelper.GetStaticFile("IStockClientStatic/tcxg/css/tcxg_tint.css");

            if (!string.IsNullOrEmpty(Request["color"]) && Request["color"] == "black")
            {
                css = StringHelper.GetStaticFile("IStockClientStatic/tcxg/css/tcxg.css");
                ViewBag.Color = "black";
            }
            else
            {
                ViewBag.Color = "default";
            }

            ViewBag.PageCss = css;
            ViewBag.Token = Request["token"];

            return View();
        }

        /// <summary>
        /// 获取题材领涨战绩
        /// </summary>
        /// <returns></returns>
        public string GetTclzRecord(string sortFlag, string sortType)
        {
            if (!TokenBll.CheckRightsByCookie(FuncRights.YJJX))
            {
                return string.Empty;
            }

            string reqFlag = Request["sortFlag"];
            string reqType = Request["sortType"];

            if (string.IsNullOrEmpty(reqFlag) || string.IsNullOrEmpty(reqType))
            {
                return string.Empty;
            }

            TclzBll bll = new TclzBll();

            List<VM_CHASE_STK_LIST> list = bll.GetRecord(reqFlag, reqType);

            if (list != null && list.Count > 0)
            {
                return JsonHelper.ToJson(list);
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 获取题材领涨成功率
        /// </summary>
        /// <returns></returns>
        public string GetTclzSuccessrate()
        {
            if (!TokenBll.CheckRightsByCookie(FuncRights.YJJX))
            {
                return string.Empty;
            }

            TclzBll bll = new TclzBll();

            List<CHASE_SUCCESSRATE> list = bll.GetSuccessRate();

            if (list != null && list.Count > 0)
            {
                return JsonHelper.ToJson(list);
            }
            else
            {
                return string.Empty;
            }
        }
        #endregion
    }
}
